package yl.hs.bmipfull.controllers.production;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.pojo.production.TransTaskEntity;
import yl.hs.bmipfull.pojo.vehiclemanage.TruckInfoEntity;
import yl.hs.bmipfull.service.production.ITransTaskService;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.service.vehiclemanage.ITruckInfoService;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.TruckState;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 作用:  运输任务,商砼可以拆为2条派车任务控制层<br/>
 * 创建时间: 2021年06月19日 09时30分07秒 <br/>
 *
 * @author 谢东
 */
@Api(value = "运输任务Web接口", tags = "生产管理——运输任务")
@RequestMapping(value = "/api/transTask")
@RestController
public class TransTaskController extends ControllerBase {

    @Autowired
    ITransTaskService service;
    @Autowired
    ITruckInfoService truckInfoService;
    @Autowired
    ISearchViewService svSVC;
    @Autowired
    ITruckInfoService truckinfo;

    @ApiOperation(value = "查询运输任务,商砼可以拆为2条派车任务", notes = "查询运输任务,商砼可以拆为2条派车任务")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询运输任务,商砼可以拆为2条派车任务", notes = "分页查询运输任务,商砼可以拆为2条派车任务")
    @RequestMapping(value = "/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var result = service.queryPage(query, rowNum, page);
            svSVC.updateUserMenuView(query);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }


    @ApiOperation(value = "查询运输任务,商砼可以拆为2条派车任务", notes = "根据运输任务,商砼可以拆为2条派车任务编号查询运输任务,商砼可以拆为2条派车任务")
    @RequestMapping(value = "/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", service.findBySn(sn));
    }

    @ApiOperation(value = "查询运输任务,商砼可以拆为2条派车任务", notes = "根据生产任务编号查询运输任务")
    @RequestMapping(value = "getByTaskSN/{taskSN}", method = RequestMethod.GET)
    public WebResult findByTaskSN(@ApiParam(value = "生产任务编号") @PathVariable String taskSN) {
        return success("查询成功", service.findByTaskSN(taskSN));
    }


    @ApiOperation(value = "保存运输任务,商砼可以拆为2条派车任务", notes = "查询/更新运输任务,商砼可以拆为2条派车任务信息")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public WebResult saveMenu(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            //检查车辆是否已有派车信息，避免重读派车
            var ss = service.queryListByTaskCar(submitModel.getData().get("truckCode").toString());
            if(ss.size()>0){
                return fail("车辆已派");
            }
            result = service.saveTransTask(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else if(result==-10){
                return fail("此合同达到控制值！");
            }else{
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "保存派车任务集合", notes = "保存派车任务集合")
    @RequestMapping(value = "/saveList", method = RequestMethod.PUT)
    public WebResult saveList(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result = 0;
        try {
            List<Map<String, Object>> datas = (List<Map<String, Object>>) submitModel.getData().get("transTaskArr");
            List<TransTaskEntity> itemList = new ArrayList<>();
            for (var item : datas) {
                TransTaskEntity entity = new TransTaskEntity();
                Tl.getChangeLog(entity, item);
                itemList.add(entity);
            }
            String taskSNList="";
            String truckSn="";
            for (TransTaskEntity entity : itemList) {
                result = 0;
                taskSNList=taskSNList+","+entity.getProductionTaskSN();
                truckSn=entity.getTruckSN();
                result = service.saveTransTask(entity.toMap(), null);
                if (result > 0) {
                    Integer truckUpdate = truckInfoService.updateStateBySN(entity.getTruckSN(), TruckState.WAITING.code);
                }
            }
            TruckInfoEntity truckEntity=truckInfoService.findBySn(truckSn);
            truckEntity.setTaskSN(taskSNList);
            truckInfoService.saveTruckInfo(truckEntity.toMap(),truckEntity.getSn());
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }

    }

    @ApiOperation(value = "保存运输任务,商砼可以拆为2条派车任务", notes = "查询/更新运输任务,商砼可以拆为2条派车任务信息")
    @RequestMapping(value = "/updateTaskSN/{sn}/{taskSn}", method = RequestMethod.POST)
    public WebResult updateTaskSN(@ApiParam(value = "编号") @PathVariable String sn,
                                  @ApiParam(value = "编号") @PathVariable String taskSn) {
        Integer result;
        try {
            result = service.updateTaskSnBySN(sn, taskSn);
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }

    }

    @ApiOperation(value = "删除运输任务,商砼可以拆为2条派车任务信息", notes = "根据运输任务,商砼可以拆为2条派车任务编号删除系运输任务,商砼可以拆为2条派车任务信息")
    @RequestMapping(path = "", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "运输任务,商砼可以拆为2条派车任务编号", name = "sn", allowMultiple = true, required = true) @RequestParam("sn") String[] sn) {
        if (service.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }

    }

    @ApiOperation(value = "撤回运输任务", notes = "根据派车任务编号撤回运输任务")
    @RequestMapping(path = "/back/{sn}", method = RequestMethod.POST)
    public WebResult backTrans(@ApiParam(value = "编号") @PathVariable String sn) {
        if (service.backTransTask(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }

    }
    @ApiOperation(value = "调整派车顺序", notes = "根据派车编号调整派车顺序")
    @RequestMapping(path = "/adjustmenttrans/{lastsn}/{endsn}", method = RequestMethod.GET)
    public WebResult adjustmentTrans(@PathVariable String lastsn,@PathVariable String endsn){
        var carlast = truckinfo.findBySn(lastsn);
        var carend = truckinfo.findBySn(endsn);
        var last = service.queryListByTaskCar(carlast.getTruckCode()).get(0);
        var end=service.queryListByTaskCar(carend.getTruckCode()).get(0);
        last.setDispatchTime(new Date(end.getDispatchTime().getTime()-1000));
        var result = service.upDate(last);
        return  success("更改成功");
    }
}